package cn.hongyu.dao.impl;

import cn.hongyu.dao.RoleDao;
import cn.hongyu.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class RoleDaoImpl implements RoleDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<Role> list() {
        List<Role> list = jdbcTemplate.query("select * from sys_role", new BeanPropertyRowMapper<Role>(Role.class));
        return list;
    }

    @Override
    public List<Role> fingById(Long id) {
        List<Role> roles = jdbcTemplate.query("select * from sys_user_role ur,sys_role r where ur.roleId=r.id and ur.userId=?",
                new BeanPropertyRowMapper<Role>(Role.class), id);
        return roles;
    }

    @Override
    public void save(Role role) {
        int update = jdbcTemplate.update("insert into sys_role values(?,?,?)", null, role.getRoleName(), role.getRoleDesc());
        System.out.println(update);
    }

    @Override
    public void delete(Long id) {
        jdbcTemplate.update("delete from sys_user_role where roleid=?",id);
        jdbcTemplate.update("delete from sys_role where id=?",id);
    }
}
